package com.jiyi.activity.datainput.sm;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiyi.bean.datainput.new_input.BaseCheck;
import com.jiyi.bean.datainput.new_input.SpecialFireBean;
import com.jiyi.exception.SampleProgressObserver;
import com.jiyi.retrofit.HttpMethods;
import com.jiyi.widget.NoScrollGridView;
import com.jiyi.widget.picselect.PicsAdapter;
import com.jiyi.widget.picshow.ImagePagerActivity;
import com.swift.base.ActivityBase;
import com.swift.maintenance.R;
import com.swift.maintenance.utils.RadioGroupUtil;
import com.swift.maintenance.utils.UserInfoUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/3/30.
 */

public class SpecialFirePreventionReadActivity extends ActivityBase {
    @BindView(R.id.tvBack)
    TextView tvBack;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvSearch)
    TextView tvSearch;
    @BindView(R.id.ivHint)
    ImageView ivHint;
    @BindView(R.id.top_layout)
    RelativeLayout topLayout;
    @BindView(R.id.rbFirePerformanceYes)
    RadioButton rbFirePerformanceYes;
    @BindView(R.id.rbFirePerformanceNo)
    RadioButton rbFirePerformanceNo;
    @BindView(R.id.rgFirePerformanceIsOK)
    RadioGroup rgFirePerformanceIsOK;
    @BindView(R.id.tvFirePerformanceProblem)
    TextView tvFirePerformanceProblem;
    @BindView(R.id.tvFirePerformanceMeasure)
    TextView tvFirePerformanceMeasure;
    @BindView(R.id.rbCableWellYes)
    RadioButton rbCableWellYes;
    @BindView(R.id.rbCableWellNo)
    RadioButton rbCableWellNo;
    @BindView(R.id.rgCableWellIsOK)
    RadioGroup rgCableWellIsOK;
    @BindView(R.id.tvCableWellProblem)
    TextView tvCableWellProblem;
    @BindView(R.id.tvCableWellMeasure)
    TextView tvCableWellMeasure;
    @BindView(R.id.rbFireWallLimitYes)
    RadioButton rbFireWallLimitYes;
    @BindView(R.id.rbFireWallLimitNo)
    RadioButton rbFireWallLimitNo;
    @BindView(R.id.rgFireWallLimitIsOK)
    RadioGroup rgFireWallLimitIsOK;
    @BindView(R.id.tvFireWallLimitProblem)
    TextView tvFireWallLimitProblem;
    @BindView(R.id.tvFireWallLimitMeasure)
    TextView tvFireWallLimitMeasure;
    @BindView(R.id.rbFireBlockYes)
    RadioButton rbFireBlockYes;
    @BindView(R.id.rbFireBlockNo)
    RadioButton rbFireBlockNo;
    @BindView(R.id.rgFireBlockIsOK)
    RadioGroup rgFireBlockIsOK;
    @BindView(R.id.tvFireBlockProblem)
    TextView tvFireBlockProblem;
    @BindView(R.id.tvFireBlockMeasure)
    TextView tvFireBlockMeasure;
    @BindView(R.id.rbTubeWellsUseYes)
    RadioButton rbTubeWellsUseYes;
    @BindView(R.id.rbTubeWellsUseNo)
    RadioButton rbTubeWellsUseNo;
    @BindView(R.id.rgTubeWellsUse)
    RadioGroup rgTubeWellsUse;
    @BindView(R.id.tvTubeWellsUseProblem)
    TextView tvTubeWellsUseProblem;
    @BindView(R.id.tvTubeWellsUseMeasure)
    TextView tvTubeWellsUseMeasure;
    @BindView(R.id.rbElectricLineYes)
    RadioButton rbElectricLineYes;
    @BindView(R.id.rbElectricLineNo)
    RadioButton rbElectricLineNo;
    @BindView(R.id.rgElectricLineIsOk)
    RadioGroup rgElectricLineIsOk;
    @BindView(R.id.tvElectricLineProblem)
    TextView tvElectricLineProblem;
    @BindView(R.id.tvElectricLineMeasure)
    TextView tvElectricLineMeasure;
    @BindView(R.id.rbElectricityLoadYes)
    RadioButton rbElectricityLoadYes;
    @BindView(R.id.rbElectricityLoadNo)
    RadioButton rbElectricityLoadNo;
    @BindView(R.id.rgElectricityLoadIsOk)
    RadioGroup rgElectricityLoadIsOk;
    @BindView(R.id.tvElectricityLoadProblem)
    TextView tvElectricityLoadProblem;
    @BindView(R.id.tvElectricityLoadMeasure)
    TextView tvElectricityLoadMeasure;
    @BindView(R.id.rbGasYes)
    RadioButton rbGasYes;
    @BindView(R.id.rbGasNo)
    RadioButton rbGasNo;
    @BindView(R.id.rgGasIsOk)
    RadioGroup rgGasIsOk;
    @BindView(R.id.tvGasProblem)
    TextView tvGasProblem;
    @BindView(R.id.tvGasMeasure)
    TextView tvGasMeasure;

    String build_id;
    @BindView(R.id.gvFirePerformance)
    NoScrollGridView gvFirePerformance;
    @BindView(R.id.gvCableWell)
    NoScrollGridView gvCableWell;
    @BindView(R.id.gvFireWallLimit)
    NoScrollGridView gvFireWallLimit;
    @BindView(R.id.gvFireBlock)
    NoScrollGridView gvFireBlock;
    @BindView(R.id.gvTubeWellsUse)
    NoScrollGridView gvTubeWellsUse;
    @BindView(R.id.gvElectricLine)
    NoScrollGridView gvElectricLine;
    @BindView(R.id.gvElectricityLoad)
    NoScrollGridView gvElectricityLoad;
    @BindView(R.id.gvGas)
    NoScrollGridView gvGas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_special_fire_prevention_read);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        getIntentData();
        setTop();
        setViewStatus();

        if (!TextUtils.isEmpty(build_id)) {
            //修改时，获取数据
            getData();
        }
    }

    private void getIntentData() {
        build_id = getIntent().getStringExtra("build_no");
        if (TextUtils.isEmpty(build_id))
            build_id = "";
    }

    private void setTop() {
        tvTitle.setText("专项防火情况");
        tvSearch.setVisibility(View.GONE);
    }

    private void setViewStatus() {
        RadioGroupUtil.disableRadioGroup(rgCableWellIsOK);
        RadioGroupUtil.disableRadioGroup(rgElectricityLoadIsOk);
        RadioGroupUtil.disableRadioGroup(rgElectricLineIsOk);
        RadioGroupUtil.disableRadioGroup(rgFireBlockIsOK);
        RadioGroupUtil.disableRadioGroup(rgFirePerformanceIsOK);
        RadioGroupUtil.disableRadioGroup(rgFireWallLimitIsOK);
        RadioGroupUtil.disableRadioGroup(rgGasIsOk);
        RadioGroupUtil.disableRadioGroup(rgTubeWellsUse);
    }

    public void getData() {
        HttpMethods.getInstance().getSpecialFireInfo(UserInfoUtil.getCurrentUserAccessToken(), build_id, 2)
                .subscribe(new SampleProgressObserver<SpecialFireBean>(this) {
                    @Override
                    public void onNext(SpecialFireBean bean) {
                        if (bean == null || "".equals(bean.getGson_null())) {
                            bean = new SpecialFireBean();
                            BaseCheck bc1 = new BaseCheck();
                            bc1.setIs_ok(0);
                            bc1.setProblem("");
                            bc1.setMeasures("");
                            bean.setWall_fireproof_performance(bc1);
                            BaseCheck bc2 = new BaseCheck();
                            bc2.setIs_ok(0);
                            bc2.setProblem("");
                            bc2.setMeasures("");
                            bean.setCable_pipe_independent(bc2);
                            BaseCheck bc3 = new BaseCheck();
                            bc3.setIs_ok(0);
                            bc3.setProblem("");
                            bc3.setMeasures("");
                            bean.setWell_pipe_reach(bc3);
                            BaseCheck bc4 = new BaseCheck();
                            bc4.setIs_ok(0);
                            bc4.setProblem("");
                            bc4.setMeasures("");
                            bean.setCable_pipe_sealed(bc4);
                            BaseCheck bc5 = new BaseCheck();
                            bc5.setIs_ok(0);
                            bc5.setProblem("");
                            bc5.setMeasures("");
                            bean.setPipe_well_occupation(bc5);
                            BaseCheck bc6 = new BaseCheck();
                            bc6.setIs_ok(0);
                            bc6.setProblem("");
                            bc6.setMeasures("");
                            bean.setElectrical_wiring_equipment(bc6);
                            BaseCheck bc7 = new BaseCheck();
                            bc7.setIs_ok(0);
                            bc7.setProblem("");
                            bc7.setMeasures("");
                            bean.setFire_power_load(bc7);
                            BaseCheck bc8 = new BaseCheck();
                            bc8.setIs_ok(0);
                            bc8.setProblem("");
                            bc8.setMeasures("");
                            bean.setGas_site_standards(bc8);
                        }

                        fillData(bean);
                    }
                });
    }

    private void fillData(SpecialFireBean bean) {
        if (bean.getWall_fireproof_performance().getIs_ok() == 1) {
            rbFirePerformanceYes.setChecked(true);
        }

        tvFirePerformanceProblem.setText(bean.getWall_fireproof_performance().getProblem());
        tvFirePerformanceMeasure.setText(bean.getWall_fireproof_performance().getMeasures());

        if (bean.getCable_pipe_independent().getIs_ok() == 1) {
            rbCableWellYes.setChecked(true);
        }

        tvCableWellProblem.setText(bean.getCable_pipe_independent().getProblem());
        tvCableWellMeasure.setText(bean.getCable_pipe_independent().getMeasures());

        if (bean.getWell_pipe_reach().getIs_ok() == 1) {
            rbFireWallLimitYes.setChecked(true);
        }

        tvFireWallLimitProblem.setText(bean.getWell_pipe_reach().getProblem());
        tvFireWallLimitMeasure.setText(bean.getWell_pipe_reach().getMeasures());

        if (bean.getCable_pipe_sealed().getIs_ok() == 1) {
            rbFireBlockYes.setChecked(true);
        }

        tvFireBlockProblem.setText(bean.getCable_pipe_sealed().getProblem());
        tvFireBlockMeasure.setText(bean.getCable_pipe_sealed().getMeasures());

        if (bean.getPipe_well_occupation().getIs_ok() == 1) {
            rbTubeWellsUseYes.setChecked(true);
        }

        tvTubeWellsUseProblem.setText(bean.getPipe_well_occupation().getProblem());
        tvTubeWellsUseMeasure.setText(bean.getPipe_well_occupation().getMeasures());

        if (bean.getElectrical_wiring_equipment().getIs_ok() == 1) {
            rbElectricLineYes.setChecked(true);
        }

        tvElectricLineProblem.setText(bean.getElectrical_wiring_equipment().getProblem());
        tvElectricLineMeasure.setText(bean.getElectrical_wiring_equipment().getMeasures());

        if (bean.getFire_power_load().getIs_ok() == 1) {
            rbElectricityLoadYes.setChecked(true);
        }

        tvElectricityLoadProblem.setText(bean.getFire_power_load().getProblem());
        tvElectricityLoadMeasure.setText(bean.getFire_power_load().getMeasures());

        if (bean.getGas_site_standards().getIs_ok() == 1) {
            rbGasYes.setChecked(true);
        }

        tvGasProblem.setText(bean.getGas_site_standards().getProblem());
        tvGasMeasure.setText(bean.getGas_site_standards().getMeasures());

        initGridViewList(bean);
    }

    void initGridViewList(SpecialFireBean specialFireBean)
    {
        if (specialFireBean.getWall_fireproof_performance().getImage() == null || specialFireBean.getWall_fireproof_performance().getImage().size() <= 0) {
            gvFirePerformance.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvFirePerformance,specialFireBean.getWall_fireproof_performance().getImage());
        }

        if (specialFireBean.getCable_pipe_independent().getImage() == null || specialFireBean.getCable_pipe_independent().getImage().size() <= 0) {
            gvCableWell.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvCableWell,specialFireBean.getCable_pipe_independent().getImage());
        }

        if (specialFireBean.getWell_pipe_reach().getImage() == null || specialFireBean.getWell_pipe_reach().getImage().size() <= 0) {
            gvFireWallLimit.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvFireWallLimit,specialFireBean.getWell_pipe_reach().getImage());
        }

        if (specialFireBean.getCable_pipe_sealed().getImage() == null || specialFireBean.getCable_pipe_sealed().getImage().size() <= 0) {
            gvFireBlock.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvFireBlock,specialFireBean.getCable_pipe_sealed().getImage());
        }

        if (specialFireBean.getPipe_well_occupation().getImage() == null || specialFireBean.getPipe_well_occupation().getImage().size() <= 0) {
            gvTubeWellsUse.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvTubeWellsUse,specialFireBean.getPipe_well_occupation().getImage());
        }

        if (specialFireBean.getElectrical_wiring_equipment().getImage() == null || specialFireBean.getElectrical_wiring_equipment().getImage().size() <= 0) {
            gvElectricLine.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvElectricLine,specialFireBean.getElectrical_wiring_equipment().getImage());
        }

        if (specialFireBean.getFire_power_load().getImage() == null || specialFireBean.getFire_power_load().getImage().size() <= 0) {
            gvElectricityLoad.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvElectricityLoad,specialFireBean.getFire_power_load().getImage());
        }

        if (specialFireBean.getGas_site_standards().getImage() == null || specialFireBean.getGas_site_standards().getImage().size() <= 0) {
            gvGas.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvGas,specialFireBean.getGas_site_standards().getImage());
        }

    }

    void setGridviewAdapter(GridView gridView, List<String> picList)
    {
        gridView.setVisibility(View.VISIBLE);
        //图片列表
        if (picList != null) {
            final String [] urls = new String [picList.size()];
            for (int i = 0; i < picList.size(); i++) {
                urls[i] = picList.get(i);
            }

            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    Intent intent = new Intent(view.getContext(),ImagePagerActivity.class);
                    intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls);
                    intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
                    view.getContext().startActivity(intent);
                }
            });
            PicsAdapter picsAdapter = new PicsAdapter(gridView.getContext(),picList);
            gridView.setAdapter(picsAdapter);
        }
    }

    @OnClick({R.id.tvBack})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvBack:
                finish();
                break;
            default:
                break;
        }
    }
}
